package com.cash.channel.svc.service;

import com.aliyuncs.utils.StringUtils;
import com.cash.channel.api.exception.CashChannelException;
import com.cash.channel.api.model.channel.ChannelModel;
import com.cash.channel.api.model.channel.RequireParam;
import com.cash.channel.api.service.ChannelService;
import com.cash.channel.svc.annotation.ServiceLog;
import com.cash.channel.svc.logic.ChannelLogic;
import com.cash.common.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Slf4j
@Service("channelService")
public class ChannelServiceImpl implements ChannelService {

    @Autowired
    private ChannelLogic channelLogic;

    @Override
    @ServiceLog("保存渠道信息")
    public Result<ChannelModel> saveChannelInfoByCode(ChannelModel channelModel) throws CashChannelException {
        if (StringUtils.isEmpty(channelModel.getcChannel())) {
            return Result.failed("渠道号不能为空");
        }
        return Result.succeed(channelLogic.saveChannelInfoByCode(channelModel));

    }

    @Override
    @ServiceLog("查找渠道信息修改日志列表")
    public Result<List> findChangeLogList(String channelCode) {
        if (StringUtils.isEmpty(channelCode)) {
            return Result.failed("渠道号不能为空");
        }
        return Result.succeed(channelLogic.findChangeLogList(channelCode));
    }

    @Override
    @ServiceLog("查找渠道信息修改日志")
    public Result getChangeLogByChangeId(Integer changeId) {
        if (null==changeId||changeId==0) {
            return Result.failed("渠道id不能为空");
        }
        return Result.succeed(channelLogic.getChangeLogModelById(changeId));
    }

    @Override
    @ServiceLog("查找渠道信息列表")
    public Result<Map<String, Object>> findChangeList(String channelName, Integer status, Integer page, Integer rows) throws CashChannelException {
        return Result.succeed(channelLogic.findChangeList(channelName, status, page, rows));
    }

    @Override
    @ServiceLog("查找渠道信息")
    public Result<ChannelModel> findChannelModelByCodeWithCreditPlatform(String channelCode) throws CashChannelException {
        return Result.succeed(channelLogic.findChannelModelByCodeWithCreditPlatform(channelCode));
    }

    @Override
    @ServiceLog("查找渠道必填字段")
    public RequireParam findApplySubmitRequireParam(String channelCode) throws CashChannelException {
        return channelLogic.checkParamsRequire(channelCode);
    }

    @Override
    @ServiceLog("检查渠道当日限额是否足够")
    public void checkChannelQuota(String channel, BigDecimal loanAmount) throws CashChannelException {
        channelLogic.checkChannelQuota(channel, loanAmount);
    }

    @Override
    public String getChannelCallbackUrl(String channel, String type) throws CashChannelException {
        if (StringUtils.isEmpty(channel) || StringUtils.isEmpty(type)) {
            throw new CashChannelException("回调配置错误，请联系管理员");
        }
        String url = channelLogic.getChannelCallbackUrl(channel, type);
        if (StringUtils.isEmpty(url)) {
            log.error("未配置渠道"+channel+"下 ["+type+"]链接");
            throw new CashChannelException("回调配置错误，请联系管理员");
        }
        return url;
    }

}
